//
//  EaseBubbleView+Card.m
//  ChatDemo-UI2.0
//
//  Created by jiuzhou on 16/3/16.
//  Copyright © 2016年 jiuzhou. All rights reserved.
//

#import "EaseBubbleView+Card.h"

@implementation EaseBubbleView (Card)
#pragma mark - private

- (void)_setupCardBubbleMarginConstraints
{
    NSLayoutConstraint *marginTopConstraint = [NSLayoutConstraint constraintWithItem:self.UserCard attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:self.margin.top];
    NSLayoutConstraint *marginBottomConstraint = [NSLayoutConstraint constraintWithItem:self.UserCard attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-self.margin.bottom];
    NSLayoutConstraint *marginLeftConstraint = [NSLayoutConstraint constraintWithItem:self.UserCard attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-self.margin.right];
    NSLayoutConstraint *marginRightConstraint = [NSLayoutConstraint constraintWithItem:self.UserCard attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backgroundImageView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:self.margin.left];
    
    [self.marginConstraints removeAllObjects];
    [self.marginConstraints addObject:marginTopConstraint];
    [self.marginConstraints addObject:marginBottomConstraint];
    [self.marginConstraints addObject:marginLeftConstraint];
    [self.marginConstraints addObject:marginRightConstraint];
    
    [self addConstraints:self.marginConstraints];
}

- (void)_setupCardBubbleConstraints
{
    [self _setupCardBubbleMarginConstraints];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.headImg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneNum attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneNum attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:22]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneNum attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:8]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.phoneNum attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLable attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:22]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLable attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:8]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.nameLable attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.UserCard attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.firstName attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.headImg attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
}

#pragma mark - public

- (void)setupCardBubbleView
{
    self.UserCard = [[UIImageView alloc] init];
    self.UserCard.translatesAutoresizingMaskIntoConstraints = NO;
    [self.backgroundImageView addSubview:self.UserCard];
    
    self.headImg = [[UIView alloc] init];
    self.headImg.translatesAutoresizingMaskIntoConstraints = NO;
    self.headImg.backgroundColor = HXSystemColor;
    self.headImg.layer.cornerRadius = 20;
    self.headImg.clipsToBounds = YES;
    [self.UserCard addSubview:self.headImg];
    
    self.firstName = [[UILabel alloc] init];
    self.firstName.translatesAutoresizingMaskIntoConstraints= NO;
    self.firstName.textColor = [UIColor whiteColor];
    self.firstName.font = systemFont(19);
    self.firstName.backgroundColor = [UIColor clearColor];
    self.firstName.textAlignment = NSTextAlignmentCenter;
    [self.headImg addSubview:self.firstName];
    
    self.nameLable = [[UILabel alloc] init];
    self.nameLable.translatesAutoresizingMaskIntoConstraints = NO;
    self.nameLable.font = systemFont(14);
    self.nameLable.backgroundColor = [UIColor clearColor];
    [self.UserCard addSubview:self.nameLable];
    
    self.phoneNum = [[UILabel alloc] init];
    self.phoneNum.translatesAutoresizingMaskIntoConstraints = NO;
    self.phoneNum.font = systemFont(14);
    self.phoneNum.backgroundColor = [UIColor clearColor];
    [self.UserCard addSubview:self.phoneNum];
    
    [self _setupCardBubbleConstraints];
}

- (void)updateCardMargin:(UIEdgeInsets)margin
{
    if (_margin.top == margin.top && _margin.bottom == margin.bottom && _margin.left == margin.left && _margin.right == margin.right) {
        return;
    }
    _margin = margin;
    
    [self removeConstraints:self.marginConstraints];
    [self _setupCardBubbleMarginConstraints];
}
@end
